package com.platform.car.controller;

import com.platform.car.model.Camera;
import com.platform.car.model.Lock;
import com.platform.car.model.Parking;
import com.platform.car.service.CameraService;
import com.platform.car.service.LockService;
import com.platform.car.service.ParkingService;
import com.platform.common.config.Global;
import com.platform.common.page.Page;
import com.platform.common.utils.UserUtils;
import com.platform.upms.model.UpmsUser;
import com.platform.upms.model.json.Tip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/car/camera")
public class CameraController {
    @Autowired
    CameraService cameraService;

    @Autowired
    ParkingService parkingService;

    /**
     * 带分页的查询列表
     * @param page
     * @param modelMap
     * @return
     */
    @RequestMapping("/list")
    public String list(Page<Camera> page, ModelMap modelMap, Camera camera){
        if(!UserUtils.isHighestPower()) {
            camera.setOrgId(UserUtils.getUserOrgId());
        }
        Page<Camera> cameraList = cameraService.findByPage(page, camera);
        modelMap.addAttribute("cameraList",cameraList);
        return  "/car/camera/list.jsp";
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping("/add")
    public String add(ModelMap modelMap,HttpSession session){
        UpmsUser upmsUser= (UpmsUser) session.getAttribute(Global.UPMSUSER);
        Integer orgId = upmsUser.getOrgId();
        Parking p=new Parking();
        if(orgId !=null){
            p.setOrgId(orgId);
        }
        List<Parking> parkingList = parkingService.findList(p);
        modelMap.put("parkingList",parkingList);
        return "/car/camera/add.jsp";
    }

    /**
     * 保存数据
     * @param camera
     * @return
     */
    @RequestMapping("/saveData")
    @ResponseBody
    public Tip saveData(Camera camera){
        try{
            cameraService.saveData(camera);
            return  new Tip("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Tip(1,"保存失败");
        }
    }

    /**
     * 修改页面
     * @param id
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id,ModelMap modelMap,HttpSession session){
        Camera camera=cameraService.get(id);
        UpmsUser upmsUser= (UpmsUser) session.getAttribute(Global.UPMSUSER);
        Integer orgId = upmsUser.getOrgId();
        Parking p=new Parking();
        if(orgId !=null){
            p.setOrgId(orgId);
        }
        List<Parking> parkingList = parkingService.findList(p);
        modelMap.put("parkingList",parkingList);
        modelMap.put("camera",camera);
        return "/car/camera/edit.jsp";
    }


    /**
     * 修改数据
     * @param
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Tip update(Camera camera){
        try{
            cameraService.update(camera);
            return  new Tip("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Tip(1,"修改失败");
        }
    }

    /**
     * 删除数据
     * @param ids
     * @return
     */
    @RequestMapping("/deleteDataByIds")
    @ResponseBody
    public Tip deleteDataByIds(String ids[]){
        try {
            cameraService.deleteDataByIds(ids);
            return  new Tip("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Tip(1,"删除失败");
        }
    }
}
